package com.hospital.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hospital.configure.Result;
import com.hospital.entity.HealthItem;
import com.hospital.mapper.HealthItemMapper;
import com.hospital.service.HealthItemService;
import com.hospital.utils.IDGenerator;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author WuLiting
 * @since 2021-03-24
 */
@Service
public class HealthItemServiceImpl extends ServiceImpl<HealthItemMapper, HealthItem> implements HealthItemService {

    @Resource
    HealthItemMapper healthItemMapper;

    @Override
    public Result<HealthItem> updateHealthItem(HealthItem healthItem) {
        Result<HealthItem> result = new Result<>();
        try {
            if (StringUtils.isEmpty(healthItem.getItemId())) {
                //如果ID为空，代表添加
                healthItem.setItemId(IDGenerator.getUUID("ITEM"));
                healthItemMapper.insert(healthItem);
            } else {
                //如果ID不为空，代表修改
                healthItemMapper.updateById(healthItem);
            }
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<HealthItem> delHealthItem(HealthItem healthItem) {
        Result<HealthItem> result = new Result<>();
        try {
            if (StringUtils.isEmpty(healthItem.getItemId())) {
                result.setMessage("healthItemId can't be empty");
                return result;
            }
            healthItemMapper.deleteById(healthItem.getItemId());
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<HealthItem> batchDelHealthItem(HealthItem healthItem) {
        Result<HealthItem> result = new Result<>();
        try {
            if (!StringUtils.isEmpty(healthItem.getIds())) {
                String[] ids = healthItem.getIds().split(",");
                for (String id : ids) {
                    healthItemMapper.deleteById(id);
                }
            }
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<HealthItem> queryHealthItem(HealthItem healthItem) {
        Result<HealthItem> result = new Result<>();
        try {
            QueryWrapper<HealthItem> wrapper = new QueryWrapper<>();
            //查询条件拼接，仅限单表查询可用

            List<HealthItem> healthItems = healthItemMapper.selectList(wrapper);
            result.setReturnList(healthItems);
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<HealthItem> queryHealthItemByPage(HealthItem healthItem) {
        Result<HealthItem> result = new Result<>();

        try {
            if (healthItem.getCurrent() == null || healthItem.getSize() == null) {
                result.setMessage("current or size can't be null");
                return result;
            }

            QueryWrapper<HealthItem> wrapper = new QueryWrapper<>();
            //查询条件拼接，仅限单表查询可用

            IPage<HealthItem> healthItems = healthItemMapper.selectPage(new Page<>(healthItem.getCurrent(), healthItem.getSize()), wrapper);
            result.setReturnPage(healthItems);
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<HealthItem> queryHealthItemById(HealthItem healthItem) {
        Result<HealthItem> result = new Result<>();
        try {
            HealthItem returnHealthItem = healthItemMapper.selectById(healthItem.getItemId());
            result.setReturnObj(returnHealthItem);
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

}
